package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import androidx.annotation.Nullable;

import com.jwgf.Half485.modbusthreadpool.PLCData;
import com.jwgf.common.Utility;
import com.jwgf.common.ViewCache;
import com.jwgf.group.GroupMap;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.protocolstack.CommunicationStateListener.WriteFailedListener;
import com.jwgf.protocolstack.CommunicationStateListener.WriteSucceedListener;
import com.jwgf.protocolstack.ProtocolStack;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.CanCommand;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineDataAccessRules;

import java.util.ArrayList;
import java.util.List;

public class ParameterSettingInSegmentParameterFragment extends BaseFragment implements OnFocusChangeListener, OnEditorActionListener, OnClickListener, WriteFailedListener, WriteSucceedListener {
    public static final String fragmentTag = "ParameterSettingInSegmentParameterFragment";

    private EditText diameter_edit;
    private EditText traverse_edit;
    private EditText lengthCompensationSmall;
    private EditText lengthCompensationBig;


    private TextView helix_num_value;
    private TextView splicer_type_value;
    private TextView conicity_value;
    private TextView tension_pad_type_value;
    private TextView process_change_tube_value;
    private TextView arm_type_value;
    private TextView change_tube_type_value;

    private ImageButton btn_helix_num;
    private ImageButton btn_splicer_type;
    private ImageButton btn_conicity;
    private ImageButton btn_tension_pad;
    private ImageButton process_change_tube_btn;
    private ImageButton arm_type_btn;
    private ImageButton change_tube_type_btn;

    private Button wax_device_has;
    private Button wax_device_no;
    private Button move_device_has;
    private Button move_device_no;
    private Button empty_tube_kick_on;
    private Button empty_tube_kick_off;

    // 气圈跟踪
    private Button btnBalConHas;
    private Button btnBalConNo;

    private Button btnBalConOn;
    private Button btnBalConOff;

    private TextView tvAutoAlign;
    private ImageButton btnAutoAlign;

    private EditText etBobbinFallback;

    private TextView tvBalConStop;
    private ImageButton btnBalConStop;

    private Button btnAntiKinkOn;
    private Button btnAntiKinkOff;

    private EditText etAntiKinkDelayOn;
    private EditText etChangingExc;

    private Button btnFirst;
    private Button btnSecond;
    private Button btnThird;
    private RelativeLayout firstPage;
    private RelativeLayout secondPage;
    private RelativeLayout thirdPage;
    private ObservableScrollView scrollView;

    private TextView in_segment_value;
    private ImageButton btn_in_segment_right_arrow;
    private ImageButton btn_in_segment_left_arrow;
    private int segment_setting_num;
    private int grp_select = 1;

    private View v;

    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    private SpindleParameter parameter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        v = ViewCache.getInstance().getView(R.layout.fragment_parameter_setting_in_segment_parameter);
        initialViews();
        updateSegmentChange();
        return v;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
        } else {
            grp_select = 1;
            updateSegmentChange();
        }
    }

    private void initialViews() {
        in_segment_value = (TextView) v.findViewById(R.id.in_segment_value);
        in_segment_value.setText("1");

        diameter_edit = initEdittextOnCreate(R.id.diameter_value);
        traverse_edit = initEdittextOnCreate(R.id.traverse_value);
        lengthCompensationSmall = initEdittextOnCreate(R.id.length_compensation_small_value);
        lengthCompensationBig = initEdittextOnCreate(R.id.length_compensation_big_value);

        btn_helix_num = (ImageButton) v.findViewById(R.id.btn_helix_num);
        btn_helix_num.setOnClickListener(this);
        btn_splicer_type = (ImageButton) v.findViewById(R.id.btn_splicer_type);
        btn_splicer_type.setOnClickListener(this);
        btn_conicity = (ImageButton) v.findViewById(R.id.btn_conicity);
        btn_conicity.setOnClickListener(this);
        btn_tension_pad = (ImageButton) v.findViewById(R.id.btn_tension_pad);
        btn_tension_pad.setOnClickListener(this);
        process_change_tube_btn = (ImageButton) v.findViewById(R.id.process_change_tube_btn);
        process_change_tube_btn.setOnClickListener(this);
        arm_type_btn = v.findViewById(R.id.arm_type_btn);
        arm_type_btn.setOnClickListener(this);
        change_tube_type_btn = v.findViewById(R.id.change_tube_type_btn);
        change_tube_type_btn.setOnClickListener(this);

        helix_num_value = (TextView) v.findViewById(R.id.helix_num_value);
        splicer_type_value = (TextView) v.findViewById(R.id.splicer_type_value);
        conicity_value = (TextView) v.findViewById(R.id.conicity_value);
        tension_pad_type_value = (TextView) v.findViewById(R.id.tension_pad_type_value);
        process_change_tube_value = (TextView) v.findViewById(R.id.process_change_tube_value);
        arm_type_value = v.findViewById(R.id.arm_type_value);
        change_tube_type_value = v.findViewById(R.id.change_tube_type_value);

        wax_device_has = (Button) v.findViewById(R.id.wax_device_has);
        wax_device_has.setOnClickListener(this);
        wax_device_no = (Button) v.findViewById(R.id.wax_device_no);
        wax_device_no.setOnClickListener(this);
        move_device_has = (Button) v.findViewById(R.id.move_device_has);
        move_device_has.setOnClickListener(this);
        move_device_no = (Button) v.findViewById(R.id.move_device_no);
        move_device_no.setOnClickListener(this);
        empty_tube_kick_on = v.findViewById(R.id.empty_tube_kick_on);
        empty_tube_kick_on.setOnClickListener(this);
        empty_tube_kick_off = v.findViewById(R.id.empty_tube_kick_off);
        empty_tube_kick_off.setOnClickListener(this);

        // 气圈跟踪
        btnBalConHas = v.findViewById(R.id.bal_con_has);
        btnBalConHas.setOnClickListener(this);
        btnBalConNo = v.findViewById(R.id.bal_con_no);
        btnBalConNo.setOnClickListener(this);

        btnBalConOn = v.findViewById(R.id.bal_con_on);
        btnBalConOn.setOnClickListener(this);
        btnBalConOff = v.findViewById(R.id.bal_con_off);
        btnBalConOff.setOnClickListener(this);

        tvAutoAlign = v.findViewById(R.id.auto_align_value);
        btnAutoAlign = v.findViewById(R.id.auto_align_btn);
        btnAutoAlign.setOnClickListener(this);

        etBobbinFallback = initEdittextOnCreate(R.id.bobbin_fallback_value);

        tvBalConStop = v.findViewById(R.id.bal_con_stop_value);
        btnBalConStop = v.findViewById(R.id.bal_con_stop_btn);
        btnBalConStop.setOnClickListener(this);

        btnAntiKinkOn = v.findViewById(R.id.anti_kink_switch_on);
        btnAntiKinkOn.setOnClickListener(this);
        btnAntiKinkOff = v.findViewById(R.id.anti_kink_switch_off);
        btnAntiKinkOff.setOnClickListener(this);

        etAntiKinkDelayOn = initEdittextOnCreate(R.id.anti_kink_delay_opening_value);
        etChangingExc = initEdittextOnCreate(R.id.change_exc_value);

        OnClickListener lrListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_in_segment_right_arrow:
                        if ((grp_select > 0) && (grp_select < segment_setting_num)) {
                            grp_select++;
                            updateSegmentChange();
                        }
                        break;
                    case R.id.btn_in_segment_left_arrow:
                        if ((grp_select > 1) && (grp_select <= segment_setting_num)) {
                            grp_select--;
                            updateSegmentChange();
                        }
                        break;
                }
            }
        };
        btn_in_segment_right_arrow = (ImageButton) v.findViewById(R.id.btn_in_segment_right_arrow);
        btn_in_segment_right_arrow.setOnClickListener(lrListener);
        btn_in_segment_left_arrow = (ImageButton) v.findViewById(R.id.btn_in_segment_left_arrow);
        btn_in_segment_left_arrow.setOnClickListener(lrListener);

        OnClickListener scroll = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_basic_setting:
                        scrollView.smoothScrollTo(0, firstPage.getTop());
                        break;
                    case R.id.btn_spindle_configuration:
                        scrollView.smoothScrollTo(0, secondPage.getTop());
                        break;
                    case R.id.btn_bal_con:
                        scrollView.smoothScrollTo(0, thirdPage.getTop());
                        break;
                }
            }
        };
        btnFirst = v.findViewById(R.id.btn_basic_setting);
        btnFirst.setOnClickListener(scroll);
        btnSecond = v.findViewById(R.id.btn_spindle_configuration);
        btnSecond.setOnClickListener(scroll);
        btnThird = v.findViewById(R.id.btn_bal_con);
        btnThird.setOnClickListener(scroll);

        firstPage = v.findViewById(R.id.first_page);
        secondPage = v.findViewById(R.id.second_page);
        thirdPage = v.findViewById(R.id.third_page);

        scrollView = v.findViewById(R.id.scroll_view);
        scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        scrollView.setFocusable(true);
        scrollView.setFocusableInTouchMode(true);
        scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                v.requestFocusFromTouch();
                return false;
            }
        });
        scrollView.setScrollChangedListener(new ObservableScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y < secondPage.getTop()) {
                    onSetButtonTrue(btnFirst);
                } else if (y < thirdPage.getTop() && y >= secondPage.getTop()) {
                    onSetButtonTrue(btnSecond);
                } else if (y >= thirdPage.getTop()) {
                    onSetButtonTrue(btnThird);
                }
            }
        });

        btnFirst.setActivated(true);
    }

    private EditText initEdittextOnCreate(int id) {
        EditText editText = (EditText) v.findViewById(id);
        editText.setSelectAllOnFocus(true);
        editText.setOnFocusChangeListener(this);
        editText.setOnEditorActionListener(this);
        editText.setBackgroundDrawable(null);
        return editText;
    }

    private void onSetButtonTrue(Button button) {
        btnFirst.setActivated(false);
        btnSecond.setActivated(false);
        btnThird.setActivated(false);
        button.setActivated(true);
    }

    private void updateViews(SpindleParameter parameter) {
        if (btnFirst.isActivated()) {
            diameter_edit.setFocusableInTouchMode(true);
            diameter_edit.requestFocusFromTouch();
        } else if (btnSecond.isActivated()) {
            wax_device_has.setFocusableInTouchMode(true);
            wax_device_has.requestFocusFromTouch();
        } else if (btnThird.isActivated()) {
            btnBalConOn.setFocusableInTouchMode(true);
            btnBalConOn.requestFocusFromTouch();
        }
        segment_setting_num = GroupMap.getInstance().getGroupCount();

        diameter_edit.setText(sp.getValue(grp_select, MachineParameterSharedPreference.IN_SEGMENT_DIAMETER));
        traverse_edit.setText(sp.getValue(grp_select, MachineParameterSharedPreference.IN_SEGMENT_TRAVERSE));


        String[] content = getResources().getStringArray(R.array.helix_num);
        String s_helix = parameter.frame20DDL;
        helix_num_value.setText(content[Integer.valueOf(s_helix)]);

        content = getResources().getStringArray(R.array.tension_pad_type);
        String s_tension_pad = parameter.frame20DDH;
        tension_pad_type_value.setText(content[Integer.valueOf(s_tension_pad)]);

        content = getResources().getStringArray(R.array.splicer_type);
        int splicerType = (Integer.valueOf(parameter.frame15DC) & 8) >> 3;
        splicer_type_value.setText(content[splicerType]);

        content = getResources().getStringArray(R.array.arm_type);
        int armType = Integer.valueOf(parameter.frame15DC) & 0x1;
        arm_type_value.setText(content[armType]);

        content = getResources().getStringArray(R.array.change_tube_type);
        int changeTubeType = Integer.parseInt(parameter.frame13DB);
        change_tube_type_value.setText(content[changeTubeType]);

        content = getResources().getStringArray(R.array.yes_no_choose);
        String processChangeTube = sp.getValue(String.valueOf(0x39) + "-" + 1);
        process_change_tube_value.setText(content[Integer.valueOf(processChangeTube)]);

        lengthCompensationSmall.setText(parameter.frame15DDH);
        lengthCompensationBig.setText(parameter.frame15DDL);

        content = getResources().getStringArray(R.array.conicity);
        String conicityAngle = sp.getValue(grp_select, MachineParameterSharedPreference.IN_SEGMENT_CONICITY, "0");
        conicity_value.setText(content[Integer.valueOf(conicityAngle)]);

        if (sp.getValue(MachineParameterSharedPreference.IN_SEGMENT_WAX_DEVICE_HAS, "0").equals("0")) {
            wax_device_has.setSelected(false);
            wax_device_no.setSelected(true);
        } else {
            wax_device_has.setSelected(true);
            wax_device_no.setSelected(false);
        }
        if (sp.getValue(MachineParameterSharedPreference.IN_SEGMENT_MOVE_DEVICE_HAS, "0").equals("0")) {
            move_device_has.setSelected(false);
            move_device_no.setSelected(true);
        } else {
            move_device_has.setSelected(true);
            move_device_no.setSelected(false);
        }

        if ("0".equals(parameter.etcFullDischarge)) {
            empty_tube_kick_on.setSelected(false);
            empty_tube_kick_off.setSelected(true);
        } else {
            empty_tube_kick_on.setSelected(true);
            empty_tube_kick_off.setSelected(false);
        }

        // 气圈跟踪
        boolean balCon = Utility.BitUtils.getBitValue(Integer.parseInt(parameter.frame11DB), 0);
        if (balCon) {
            btnBalConHas.setSelected(true);
            btnBalConNo.setSelected(false);
            btnThird.setVisibility(View.VISIBLE);
            thirdPage.setVisibility(View.VISIBLE);
        } else {
            btnBalConHas.setSelected(false);
            btnBalConNo.setSelected(true);
            btnThird.setVisibility(View.INVISIBLE);
            thirdPage.setVisibility(View.INVISIBLE);
        }

        boolean balConSwitch = Utility.BitUtils.getBitValue(Integer.parseInt(parameter.frame11DB), 1);
        if (balConSwitch) {
            btnBalConOn.setSelected(true);
            btnBalConOff.setSelected(false);
        } else {
            btnBalConOn.setSelected(false);
            btnBalConOff.setSelected(true);
        }

        boolean autoAlign = Utility.BitUtils.getBitValue(Integer.parseInt(parameter.frame11DB), 2);
        String[] autoAlignContent = getResources().getStringArray(R.array.auto_align);
        if (autoAlign) {
            tvAutoAlign.setText(autoAlignContent[1]);
        } else {
            tvAutoAlign.setText(autoAlignContent[0]);
        }


        etBobbinFallback.setText(parameter.frame11DCH);

        String[] balConStopContent = getResources().getStringArray(R.array.bal_con_stop);

        tvBalConStop.setText(balConStopContent[Integer.parseInt(parameter.frame11DCL)]);

        boolean antiKinkSwitch = Utility.BitUtils.getBitValue(Integer.parseInt(parameter.frame11DB), 6);
        if (antiKinkSwitch) {
            btnAntiKinkOn.setSelected(true);
            btnAntiKinkOff.setSelected(false);
        } else {
            btnAntiKinkOn.setSelected(false);
            btnAntiKinkOff.setSelected(true);
        }

        etAntiKinkDelayOn.setText(parameter.frame11DDH);
        etChangingExc.setText(parameter.frame11DDL);

    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            SmaroWindingMachineActivity.canHandStack.registerWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.registerWriteSucceedListener(this);
            String strValue = v.getText().toString();
            if (strValue.equals("")) {
                return false;
            }
            long id = GroupMap.getInstance().getSpindleParameterIdFromGroup(grp_select);
            parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
            boolean isValid = false;
            String key = "";
            switch (v.getId()) {
                case R.id.diameter_value:
                    key = MachineParameterSharedPreference.IN_SEGMENT_DIAMETER;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        sp.setValue(grp_select, key, strValue);
                    }
                    break;

                case R.id.traverse_value:
                    key = MachineParameterSharedPreference.IN_SEGMENT_TRAVERSE;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        sp.setValue(grp_select, key, strValue);
                    }
                    break;
                case R.id.length_compensation_small_value:
                    key = (0x15) + "-" + 3;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame15DDH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.length_compensation_big_value:
                    key = (0x15) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame15DDL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.bobbin_fallback_value: {
                    isValid = true;
                    if (isValid) {
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame11DCH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;
                case R.id.anti_kink_delay_opening_value: {
                    isValid = true;
                    if (isValid) {
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame11DDH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;
                case R.id.change_exc_value: {
                    isValid = true;
                    if (isValid) {
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame11DDL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;
                default:
                    break;
            }
            if (!isValid) {
                SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
                SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
                String max = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMax();
                String min = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMin();
                String warning = getResources().getString(R.string.range_warning);
                warning = String.format(warning, min, max);
                WarningDialog.show(getActivity(), warning);
                updateSegmentChange();
            }
        }
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            updateSegmentChange();
        }
    }

    @Override
    public void onClick(View v) {
        long id = GroupMap.getInstance().getSpindleParameterIdFromGroup(grp_select);
        parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        switch (v.getId()) {
            case R.id.btn_helix_num: {
                final String[] content = getResources().getStringArray(R.array.helix_num);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        helix_num_value.setText(content[position]);

                        String key = String.valueOf(0x20) + "-" + 2;
                        boolean isValid = false;
                        String strValue = "";
                        int value = 0;

                        if (position == 0) {//2圈
                            value = 0;
                            strValue = String.valueOf(value);
                            isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                        } else if (position == 1) {//2.5圈
                            value = 1;
                            strValue = String.valueOf(value);
                            isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                        }

                        if (isValid) {
                            SmaroWindingMachineActivity.canHandStack.registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                            SmaroWindingMachineActivity.canHandStack.registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                            parameter.frame20DDL = strValue;
                            List<byte[]> cmdList = new ArrayList<>();
                            cmdList.add(CanCommand.getBus());
                            List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                            for (Integer groupNo : groupNos) {
                                cmdList.addAll(CanCommand.createGroupCmd20(groupNo, parameter));
                            }
                            cmdList.add(CanCommand.releaseBus());
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                            WaitProgressDialog.show(getActivity());
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;

            case R.id.btn_splicer_type: {
                final String[] content = getResources().getStringArray(R.array.splicer_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        splicer_type_value.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        int para0x15DC = Integer.valueOf(parameter.frame15DC);
                        para0x15DC = Utility.BitUtils.setBitValue(para0x15DC, 3, position);
                        parameter.frame15DC = String.valueOf(para0x15DC);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;
            case R.id.btn_tension_pad: {
                final String[] content = getResources().getStringArray(R.array.tension_pad_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        tension_pad_type_value.setText(content[position]);
                        String key = String.valueOf(0x20) + "-" + 3;
                        boolean isValid = false;
                        String strValue = "";

                        strValue = String.valueOf(position);
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);

                        if (isValid) {
                            SmaroWindingMachineActivity.canHandStack.registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                            SmaroWindingMachineActivity.canHandStack.registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                            parameter.frame20DDH = strValue;
                            List<byte[]> cmdList = new ArrayList<>();
                            cmdList.add(CanCommand.getBus());
                            List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                            for (Integer groupNo : groupNos) {
                                cmdList.addAll(CanCommand.createGroupCmd20(groupNo, parameter));
                            }
                            cmdList.add(CanCommand.releaseBus());
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                            WaitProgressDialog.show(getActivity());
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }

            break;
            case R.id.process_change_tube_btn: {
                final String[] content = getResources().getStringArray(R.array.yes_no);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        process_change_tube_value.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                        SmaroWindingMachineActivity.canHandStack
                                .putPriorityCmd(sp.createCanCmdNoGroup(0x39, 0x01, String.valueOf(position)));
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;
            case R.id.btn_conicity: {
                final String[] content = getResources().getStringArray(R.array.conicity);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        conicity_value.setText(content[position]);
                        sp.setValue(grp_select, MachineParameterSharedPreference.IN_SEGMENT_CONICITY, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;
            case R.id.arm_type_btn: {
                final String[] content = getResources().getStringArray(R.array.arm_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        arm_type_value.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        int armType = Integer.valueOf(sp.getValue(String.valueOf(0x15) + "-" + 1));
                        armType = Utility.BitUtils.setBitValue(armType, 0, position);
                        parameter.frame15DC = String.valueOf(armType);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;
            case R.id.change_tube_type_btn: {
                final String[] content = getResources().getStringArray(R.array.change_tube_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        change_tube_type_value.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame13DB = String.valueOf(position);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd13(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
            break;
            case R.id.wax_device_no:
                wax_device_has.setSelected(false);
                wax_device_no.setSelected(true);
                sp.setValue(MachineParameterSharedPreference.IN_SEGMENT_WAX_DEVICE_HAS, "0");
                break;
            case R.id.wax_device_has:
                wax_device_has.setSelected(true);
                wax_device_no.setSelected(false);
                sp.setValue(MachineParameterSharedPreference.IN_SEGMENT_WAX_DEVICE_HAS, "1");
                break;
            case R.id.move_device_no:
                move_device_has.setSelected(false);
                move_device_no.setSelected(true);
                sp.setValue(MachineParameterSharedPreference.IN_SEGMENT_MOVE_DEVICE_HAS, "0");
                break;
            case R.id.move_device_has:
                move_device_has.setSelected(true);
                move_device_no.setSelected(false);
                sp.setValue(MachineParameterSharedPreference.IN_SEGMENT_MOVE_DEVICE_HAS, "1");
                break;
            case R.id.empty_tube_kick_on: {
                empty_tube_kick_on.setSelected(true);
                empty_tube_kick_off.setSelected(false);
                parameter.etcFullDischarge = "1";
                ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd37(groupNo, 0));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.empty_tube_kick_off: {
                empty_tube_kick_on.setSelected(false);
                empty_tube_kick_off.setSelected(true);
                parameter.etcFullDischarge = "0";
                ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                int emptyTubeChannelState = PLCData.getInstance().getHoldingRegister(1, 334);
                boolean bool = Utility.BitUtils.getBitValue(emptyTubeChannelState, 14);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    if (bool) {
                        cmdList.addAll(CanCommand.createGroupCmd37(groupNo, 1));
                    } else {
                        cmdList.addAll(CanCommand.createGroupCmd37(groupNo, 0));
                    }
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.bal_con_has: {
                btnBalConHas.setSelected(true);
                btnBalConNo.setSelected(false);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 0, 1);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.bal_con_no: {
                btnBalConHas.setSelected(false);
                btnBalConNo.setSelected(true);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 0, 0);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.bal_con_on: {
                btnBalConOn.setSelected(true);
                btnBalConOff.setSelected(false);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 1, 1);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.bal_con_off: {
                btnBalConOn.setSelected(false);
                btnBalConOff.setSelected(true);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 1, 0);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.auto_align_btn: {
                final String[] content = getResources().getStringArray(R.array.auto_align);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        tvAutoAlign.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        int frame11DB = Integer.parseInt(parameter.frame11DB);
                        frame11DB = Utility.BitUtils.setBitValue(frame11DB, 2, position);
                        parameter.frame11DB = String.valueOf(frame11DB);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.anti_kink_switch_on: {
                btnAntiKinkOn.setSelected(true);
                btnAntiKinkOff.setSelected(false);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 6, 1);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.anti_kink_switch_off: {
                btnAntiKinkOn.setSelected(false);
                btnAntiKinkOff.setSelected(true);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                int frame11DB = Integer.parseInt(parameter.frame11DB);
                frame11DB = Utility.BitUtils.setBitValue(frame11DB, 6, 0);
                parameter.frame11DB = String.valueOf(frame11DB);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.bal_con_stop_btn: {
                final String[] content = getResources().getStringArray(R.array.bal_con_stop);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        tvBalConStop.setText(content[position]);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingInSegmentParameterFragment.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingInSegmentParameterFragment.this);
                        parameter.frame11DCL = String.valueOf(position);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd11(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            default:
                break;
        }
    }

    private void updateSegmentChange() {
        in_segment_value.setText(String.valueOf(grp_select));
        long id = GroupMap.getInstance().getSpindleParameterIdFromGroup(grp_select);
        SpindleParameter parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        updateViews(parameter);

    }

    @Override
    public void writeSucceed(ProtocolStack ps, byte[] request) {
        short index = (short) (request[2] & 0xff);
        if (index == 0x20 | index == 0x13 | index == 0x11) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
        }
        if (index == 0x39 || index == 0x15) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
            sp.setValue(index, request);
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateSegmentChange();
            }
        });
    }

    @Override
    public void writeFailed(ProtocolStack ps, byte[] request) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateSegmentChange();
            }
        });
    }
}

